package org.qydjk.console.controller.special;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.*;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.ThumbnailatorUtils;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ASUS on 2017/12/18.
 */
@Controller
@RequestMapping("/special")
public class SpecialController extends BaseController {
    /**
     * 专题跳转
     * @return
     */
    @RequestMapping("/special.do")
    public String special() {
        return "special/special/special";
    }

    /**
     * 日期数据加载
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @param typeValue
     * @param specialCityIdSearchWord
     * @param locPlatformSearchWord
     * @param locTypeSearchWord
     * @param locNameSearchWord
     * @return
     */
    @RequestMapping("/locDataGrid.do")
    @ResponseBody
    public Object locDataGrid(Integer page, Integer rows, String sort, String order,String typeValue,String specialCityIdSearchWord,String locPlatformSearchWord,String locTypeSearchWord, String locNameSearchWord) {
        try {
            HibernatePage pager = new HibernatePage(page, rows);

            String findSpecialLocSql = "SELECT * FROM " +
                    " qy_special_loc l WHERE 1 = 1 ";
            String countSql = "SELECT count(l.id) FROM " +
                    " qy_special_loc l  WHERE 1 = 1 ";

            StringBuilder conditionSql = new StringBuilder("");
           /* if (specialCityIdSearchWord != null && !"".equals(specialCityIdSearchWord)) {
                conditionSql.append( " AND l.city_id IN ( " +
                        "   SELECT a.id FROM qy_area a " +
                        "   WHERE a.`level`=2  " +
                        "   AND (a.id= " + specialCityIdSearchWord + " " +
                        "   OR a.parent_id = " + specialCityIdSearchWord + ") " +
                        " )");
            }*/
            if (locPlatformSearchWord != null && !"".equals(locPlatformSearchWord)) {
                conditionSql.append(" AND l.platform = " + locPlatformSearchWord);
            }
            if (locTypeSearchWord != null && !"".equals(locTypeSearchWord)) {
                conditionSql.append(" AND l.type = " + locTypeSearchWord);
            }
            if (locNameSearchWord != null && !"".equals(locNameSearchWord)) {
                conditionSql.append(" AND l.name LIKE '%" + locNameSearchWord+"%'");
            }

            conditionSql.append(" AND l.type IN ("+typeValue+")");
            conditionSql.append(" ORDER BY l.ord");
            findSpecialLocSql = findSpecialLocSql + conditionSql;
            countSql = countSql + conditionSql;
            List<Map<String, Object>> list = baseService.findMapBySQL(findSpecialLocSql, pager);
            Integer total = baseService.getRecordCountSQL(countSql);
            return new EasyUIResponse(total, list);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询专题数据
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @param specialLocIdSearchWord
     * @param specialTypeSearchWord
     * @param specialTitleSearchWord
     * @param specialDesSearchWord
     * @return
     */
    @RequestMapping("/specialDataGrid.do")
    @ResponseBody
    public Object specialDataGrid(Integer page, Integer rows, String sort, String order, String specialLocIdSearchWord, String specialTypeSearchWord, String specialTitleSearchWord, String specialDesSearchWord,String specialCityIdSearchWord) {
        try {

            HibernatePage pager = new HibernatePage(page, rows);
            String findSpecialLocSql = "SELECT d.*,l.name FROM " +
                    " qy_special_detail d,qy_special_loc l " +
                    " WHERE d.special_loc_id = l.id ";


            String countSql = "SELECT count(d.id) FROM " +
                    " qy_special_detail d,qy_special_loc l " +
                    " WHERE d.special_loc_id = l.id ";

            StringBuilder conditionSql = new StringBuilder("");
            if (specialLocIdSearchWord != null && !"".equals(specialLocIdSearchWord)) {
                conditionSql.append(" AND d.special_loc_id = " + specialLocIdSearchWord);
            }
            if (specialCityIdSearchWord != null && !"".equals(specialCityIdSearchWord)) {
                conditionSql.append(" AND d.city_id = " + specialCityIdSearchWord);
            }
            if (specialTitleSearchWord != null && !"".equals(specialTitleSearchWord)) {
                conditionSql.append(" AND (d.title1 LIKE '%" + specialTitleSearchWord + "%' OR d.title2 LIKE '% " + specialTitleSearchWord + "%') ");
            }
            if (specialDesSearchWord != null && !"".equals(specialDesSearchWord)) {
                conditionSql.append(" AND (d.des1 LIKE '%" + specialDesSearchWord + "%' OR d.des2 LIKE '%" + specialDesSearchWord + "%') ");
            }
            if (specialTypeSearchWord != null && !"".equals(specialTypeSearchWord)) {
                conditionSql.append(" AND d.click_type = " + specialTypeSearchWord);
            }

            conditionSql.append(" ORDER BY d." + sort + " " + order);
            findSpecialLocSql = findSpecialLocSql + conditionSql;
            countSql = countSql + conditionSql;

            List<Map<String, Object>> list = baseService.findMapBySQL(findSpecialLocSql, pager);
            int total = baseService.getRecordCountSQL(countSql);
            return new EasyUIResponse(total, list);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 去添加专题页
     *
     * @return
     */
    @RequestMapping("/specialAddPage.do")
    public String specialAddPage() {
        return "special/special/specialDetailAdd";
    }

    /**
     * 去添加专题页
     *
     * @return
     */
    @RequestMapping("/specialAdd.do")
    @ResponseBody
    public Object specialAdd(SpecialDetail specialDetail, String specialLocIdStr) {
        try {
            String[] str = specialLocIdStr.split("_");
            String specialLocId = str[0];
            String specialLocType = str[1];
            specialDetail.setSpecialLocId(Integer.valueOf(specialLocId));
            specialDetail.setType(Integer.valueOf(specialLocType));
            specialDetail.setThumbnailImg1(ThumbnailatorUtils.imgUrlToThumbnaiUrl( specialDetail.getImg1()));
            specialDetail.setThumbnailImg2(ThumbnailatorUtils.imgUrlToThumbnaiUrl( specialDetail.getImg2()));
            Date date = new Date();
            specialDetail.setCreateTime(date);
            specialDetail.setUpdateTime(date);
            baseService.save(specialDetail);
            return renderSuccess("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return renderError("添加失败");
        }
    }

    /**
     * 查询位置
     *
     * @return
     */
    @RequestMapping("/findLocList.do")
    @ResponseBody
    public Object findLocList(String typeValue) {
        try {
            String findLocSql = "SELECT CONCAT(l.id,'_',l.type) AS id,l.`name` FROM qy_special_loc l WHERE l.type IN ("+typeValue+")";
            return baseService.findMapBySQL(findLocSql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 跳转到编辑页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/specialEditPage.do")
    public String specialEditPage(Model model, Integer id) {
        try {
            SpecialDetail specialDetail = baseService.get(SpecialDetail.class, id);
            model.addAttribute("specialDetail", specialDetail);
            return "special/special/specialDetailEdit";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 编辑专题
     * @param specialDetail
     * @param specialLocIdStr
     * @return
     */
    @RequestMapping("/specialEdit.do")
    @ResponseBody
    public Object specialEdit(SpecialDetail specialDetail, String specialLocIdStr) {
        try {
            String[] str = specialLocIdStr.split("_");
            String specialLocId = str[0];
            String specialLocType = str[1];
            SpecialDetail sd = baseService.get(SpecialDetail.class, specialDetail.getId());
            BeanUtil.copyProperties(specialDetail,sd);
            sd.setSpecialLocId(Integer.valueOf(specialLocId));
            sd.setType(Integer.valueOf(specialLocType));
            sd.setThumbnailImg1(ThumbnailatorUtils.imgUrlToThumbnaiUrl(specialDetail.getImg1()));
            sd.setThumbnailImg2(ThumbnailatorUtils.imgUrlToThumbnaiUrl(specialDetail.getImg2()));
            sd.setUpdateTime(new Date());
            baseService.update(sd);
            BeanUtils.copyProperties(specialDetail,specialDetail);

            return renderSuccess("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return renderError("修改失败");
        }

    }

    /**
     * 删除专题
     * @param specialDetail
     * @return
     */
    @RequestMapping("/specialDelete.do")
    @ResponseBody
    public Object specialDelete(SpecialDetail specialDetail) {
        try {
            baseService.delete(specialDetail);
            return renderSuccess("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return renderSuccess("删除失败");
        }

    }

    /**
     * 跳转到位置添加页
     * @return
     */
    @RequestMapping("/locAddPage.do")
    public String locAddPage() {
        return "special/special/locAdd";
    }

    /**
     * 添加位置
     * @param specialLoc
     * @param addTypeVal
     * @return
     */
    @RequestMapping("/locAdd.do")
    @ResponseBody
    public Object locAdd(SpecialLoc specialLoc,String addTypeVal) {
        try {
            //传递addTypeVal代表分类被隐藏后提交的参数
            if (addTypeVal != null && !"".equals(addTypeVal.trim())){
                specialLoc.setType(Integer.valueOf(addTypeVal));
            }
            Date date = new Date();
            specialLoc.setCreateTime(date);
            specialLoc.setUpdateTime(date);
            baseService.save(specialLoc);
            return renderSuccess("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return renderError("保存失败");
        }
    }

    /**
     * 到编辑位置页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/locEditPage.do")
    public String locEditPage(Model model, Integer id) {

        try {
            SpecialLoc specialLoc = baseService.get(SpecialLoc.class, id);
            model.addAttribute("specialLoc", specialLoc);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "special/special/locEdit";
    }

    /**
     * 修改位置
     * @param specialLoc
     * @param editTypeVal
     * @return
     */
    @RequestMapping("/locEdit.do")
    @ResponseBody
    public Object locEdit(SpecialLoc specialLoc,Integer editTypeVal){
        try {
            if(specialLoc.getType() == null){
                specialLoc.setType(editTypeVal);
            }
            SpecialLoc sl = baseService.get(specialLoc.getClass(), specialLoc.getId());
            BeanUtil.copyProperties(specialLoc, sl);
            sl.setUpdateTime(new Date());
            baseService.update(sl);
            return renderSuccess("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderError("保存失败");
        }

    }

    /**
     * 删除位置
     * @param specialLoc
     * @return
     */
    @RequestMapping("/locDelete.do")
    @ResponseBody
    public Object locDelete(SpecialLoc specialLoc) {
        try {
            baseService.delete(specialLoc);
            return renderSuccess("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return renderError("删除失败");
        }
    }

    /**
     * 商家列表
     * @return
     */
    @RequestMapping("/specialSeller.do")
    public String specialSeller(){
        return "special/special/specialSeller";
    }

    /**
     * 商家列表数据
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @param sellerIdSearchWord
     * @param sellerNameSearchWord
     * @param specialLocIdSearchWord
     * @return
     */
    @RequestMapping("/specialSellerDataGrid.do")
    @ResponseBody
    public Object specialSellerDataGrid(Integer page, Integer rows, String sort, String order,String sellerIdSearchWord,String sellerNameSearchWord,String specialLocIdSearchWord  ,String specialCityIdSearchWord){

        try {
            HibernatePage pager = new HibernatePage(page, rows);

            String findSpecialSellerSql = "SELECT ls.id,l.`name` AS lname,ls.ord,s.`name` AS sname   " +
                    " FROM qy_special_loc_seller ls ,qy_seller s,qy_special_loc l " +
                    " WHERE ls.seller_id = s.id AND ls.loc_id = l.id";
            String countSql =  "SELECT COUNT(ls.id) " +
                    " FROM qy_special_loc_seller ls ,qy_seller s,qy_special_loc l " +
                    " WHERE ls.seller_id = s.id AND ls.loc_id = l.id";

            StringBuilder conditionSql = new StringBuilder("");
            if (specialLocIdSearchWord != null && !"".equals(specialLocIdSearchWord)){
                conditionSql.append(" AND  ls.loc_id = " + specialLocIdSearchWord + "");
            }
            if (specialCityIdSearchWord != null && !"".equals(specialCityIdSearchWord)){
                conditionSql.append(" AND  ls.city_id = " + specialCityIdSearchWord + "");
            }
            if (sellerIdSearchWord != null && !"".equals(sellerIdSearchWord)){
                conditionSql.append(" AND  ls.seller_id = " + sellerIdSearchWord + "");
            }else {
                if (sellerNameSearchWord != null && !"".equals(sellerNameSearchWord)) {
                    conditionSql.append(" AND  s.`name` LIKE '%" + sellerNameSearchWord + "%'");
                }
            }

            conditionSql.append(" ORDER BY ls.ord DESC");

            findSpecialSellerSql = findSpecialSellerSql+conditionSql;
            countSql = countSql+conditionSql;

            List<Map<String, Object>> list = baseService.findMapBySQL(findSpecialSellerSql, pager);
            int total = baseService.getRecordCountSQL(countSql);
            return new EasyUIResponse(total, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 到添加商家列表页
     * @return
     */
    @RequestMapping("/specialSellerAddPage.do")
    public String specialSellerAddPage(){
        return "special/special/specialSellerAdd";
    }

    /**
     * 添加商家
     * @param specialLocSeller
     * @param specialLocIdStr
     * @return
     */
    @RequestMapping("/specialSellerAdd.do")
    @ResponseBody
    public Object specialSellerAdd(SpecialLocSeller specialLocSeller,String specialLocIdStr){
        try {
            String[] str = specialLocIdStr.split("_");
            String specialLocId = str[0];
            specialLocSeller.setLocId(Integer.valueOf(specialLocId));
            Date date = new Date();
            specialLocSeller.setCreateTime(date);
            specialLocSeller.setUpdateTime(date);

            baseService.save(specialLocSeller);
            return  renderSuccess("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderError("添加失败");
        }


    }

    /**
     * 到修改商家也
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/specialSellerEditPage.do")
    public String specialSellerEditPage(Model model,Integer id){
        try {
            String sql = "SELECT ls.id,ls.seller_id, ls.ord,l.id AS lid,l.type AS ltype,l.`name` AS lname,s.`name` AS sname " +
                    " FROM qy_special_loc_seller ls,qy_special_loc l,qy_seller s " +
                    " WHERE ls.loc_id = l.id  " +
                    " AND ls.seller_id = s.id" +
                    " AND ls.id = "+id;
            List<Map<String, Object>> locSellerList = baseService.findMapBySQL(sql);
            model.addAttribute("locSellerList",locSellerList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "special/special/specialSellerEdit";
    }

    /**
     * 修改商家
     * @param specialLocSeller
     * @param specialLocIdStr
     * @return
     */
    @RequestMapping("/specialSellerEdit.do")
    @ResponseBody
    public Object specialSellerEdit(SpecialLocSeller specialLocSeller,String specialLocIdStr){
        try {
            String[] str = specialLocIdStr.split("_");
            String specialLocId = str[0];
            specialLocSeller.setLocId(Integer.valueOf(specialLocId));
            specialLocSeller.setUpdateTime(new Date());

            SpecialLocSeller ls = baseService.get(SpecialLocSeller.class, specialLocSeller.getId());
            BeanUtil.copyProperties(specialLocSeller,ls);
            baseService.update(ls);

            return  renderSuccess("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderSuccess("编辑失败");
        }
    }

    /**
     * 删除商家
     * @param specialLocSeller
     * @return
     */
    @RequestMapping("/specialSellerDelete.do")
    @ResponseBody
    public Object specialSellerDelete(SpecialLocSeller specialLocSeller){
        try {
            baseService.delete(specialLocSeller);
            return  renderSuccess("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderError("删除失败");
        }
    }


    /**
     * 产品专题
     * @return
     */
    @RequestMapping("/specialProduct.do")
    public String specialProduct(){
        return "special/special/specialProduct";
    }


    /**
     * 产品数据
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @param productIdSearchWord
     * @param productNameSearchWord
     * @param specialLocIdSearchWord
     * @return
     */
    @RequestMapping("/specialProductDataGrid.do")
    @ResponseBody
    public Object specialProductDataGrid(Integer page, Integer rows, String sort, String order,String productIdSearchWord,String productNameSearchWord,String specialLocIdSearchWord,String specialCityIdSearchWord ){
        try {
            HibernatePage pager = new HibernatePage(page, rows);

            String findSpecialProductSql = "SELECT lp.id,l.`name` AS lname,lp.ord,p.`name` AS pname     " +
                    " FROM qy_special_loc_product lp ,qy_product p,qy_special_loc l   " +
                    " WHERE lp.product_id = p.id AND lp.loc_id = l.id";
            String countSql =  "SELECT COUNT(lp.id)" +
                    " FROM qy_special_loc_product lp ,qy_product p,qy_special_loc l   " +
                    " WHERE lp.product_id = p.id AND lp.loc_id = l.id";

            StringBuilder conditionSql = new StringBuilder("");
            if (specialLocIdSearchWord != null && !"".equals(specialLocIdSearchWord)){
                conditionSql.append(" AND  lp.loc_id = " + specialLocIdSearchWord + "");
            }
            if (specialCityIdSearchWord != null && !"".equals(specialCityIdSearchWord)){
                conditionSql.append(" AND  lp.city_id = " + specialCityIdSearchWord + "");
            }
            if (productIdSearchWord != null && !"".equals(productIdSearchWord)){
                conditionSql.append(" AND  lp.product_id = " + productIdSearchWord + "");
            }else {
                if (productNameSearchWord != null && !"".equals(productNameSearchWord)) {
                    conditionSql.append(" AND  p.`name` LIKE '%" + productNameSearchWord + "%'");
                }
            }

            conditionSql.append(" ORDER BY lp.ord DESC");
            findSpecialProductSql = findSpecialProductSql+conditionSql;
            countSql = countSql+conditionSql;

            List<Map<String, Object>> list = baseService.findMapBySQL(findSpecialProductSql, pager);
            int total = baseService.getRecordCountSQL(countSql);
            return new EasyUIResponse(total, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 到添加产品页
     * @return
     */
    @RequestMapping("/specialProductAddPage.do")
    public String specialProductAddPage(){
        return "special/special/specialProductAdd";
    }

    /**
     * 添加产品
     * @param specialLocProduct
     * @param specialLocIdStr
     * @return
     */
    @RequestMapping("/specialProductAdd.do")
    @ResponseBody
    public Object specialProductAdd(SpecialLocProduct specialLocProduct, String specialLocIdStr){
        try {
            String[] str = specialLocIdStr.split("_");
            String specialLocId = str[0];
            specialLocProduct.setLocId(Integer.valueOf(specialLocId));
            Date date = new Date();
            specialLocProduct.setCreateTime(date);
            specialLocProduct.setUpdateTime(date);

            baseService.save(specialLocProduct);
            return  renderSuccess("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderError("添加失败");
        }


    }

    /**
     * 到编辑产品页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/specialProductEditPage.do")
    public String specialProductEditPage(Model model,Integer id){
        try {
            String sql = "SELECT lp.id,lp.product_id, lp.ord,l.id AS lid,l.type AS ltype,l.`name` AS lname,p.`name` AS pname  " +
                    "   FROM qy_special_loc_product lp,qy_special_loc l,qy_product p " +
                    "   WHERE lp.loc_id = l.id   " +
                    "   AND lp.product_id = p.id " +
                    "   AND lp.id = "+id;
            List<Map<String, Object>> locProductList = baseService.findMapBySQL(sql);
            model.addAttribute("locProductList",locProductList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "special/special/specialProductEdit";
    }

    /**
     * 编辑产品
     * @param specialLocProduct
     * @param specialLocIdStr
     * @return
     */
    @RequestMapping("/specialProductEdit.do")
    @ResponseBody
    public Object specialProductEdit(SpecialLocProduct specialLocProduct,String specialLocIdStr){
        try {
            String[] str = specialLocIdStr.split("_");
            String specialLocId = str[0];
            specialLocProduct.setLocId(Integer.valueOf(specialLocId));
            specialLocProduct.setUpdateTime(new Date());

            SpecialLocProduct lp = baseService.get(SpecialLocProduct.class, specialLocProduct.getId());
            BeanUtil.copyProperties(specialLocProduct,lp);
            baseService.update(lp);

            return  renderSuccess("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderSuccess("编辑失败");
        }
    }

    /**
     * 删除产品
     * @param specialLocProduct
     * @return
     */
    @RequestMapping("/specialProductDelete.do")
    @ResponseBody
    public Object specialProductDelete(SpecialLocProduct specialLocProduct){
        try {
            baseService.delete(specialLocProduct);
            return  renderSuccess("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderError("删除失败");
        }
    }


    /**
     * 文章专题
     * @return
     */
    @RequestMapping("/specialArticle.do")
    public String specialArticle(){
        return "special/special/specialArticle";
    }


    /**
     * 文章数据
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @param articleIdSearchWord
     * @param articleNameSearchWord
     * @param specialLocIdSearchWord
     * @return
     */

    @RequestMapping("/specialArticleDataGrid.do")
    @ResponseBody
    public Object specialArticleDataGrid(Integer page, Integer rows, String sort, String order,String articleIdSearchWord,String articleNameSearchWord,String specialLocIdSearchWord,String specialCityIdSearchWord ){
        try {
            HibernatePage pager = new HibernatePage(page, rows);

            String findSpecialProductSql = "SELECT la.id,l.`name` AS lname,la.ord, a.title AS title  " +
                    "FROM qy_special_loc_article la ,qy_article a,qy_special_loc l " +
                    "WHERE la.article_id = a.id AND la .loc_id = l.id";
            String countSql =  "SELECT COUNT(la.id) " +
                    "FROM qy_special_loc_article la ,qy_article a,qy_special_loc l " +
                    "WHERE la.article_id = a.id AND la .loc_id = l.id";

            StringBuilder conditionSql = new StringBuilder("");
            if (specialLocIdSearchWord != null && !"".equals(specialLocIdSearchWord)){
                conditionSql.append(" AND  la.loc_id = " + specialLocIdSearchWord + "");
            }
            if (specialCityIdSearchWord != null && !"".equals(specialCityIdSearchWord)){
                conditionSql.append(" AND  la.city_id = " + specialCityIdSearchWord + "");
            }
            if (articleIdSearchWord != null && !"".equals(articleIdSearchWord)){
                conditionSql.append(" AND  la.article_id = " + articleIdSearchWord + "");
            }else {
                if (articleNameSearchWord != null && !"".equals(articleNameSearchWord)) {
                    conditionSql.append(" AND  a.title LIKE '%" + articleNameSearchWord + "%'");
                }
            }

            conditionSql.append(" ORDER BY la.ord DESC");
            findSpecialProductSql = findSpecialProductSql+conditionSql;
            countSql = countSql+conditionSql;

            List<Map<String, Object>> list = baseService.findMapBySQL(findSpecialProductSql, pager);
            int total = baseService.getRecordCountSQL(countSql);
            return new EasyUIResponse(total, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 到添加文章页
     * @return
     */
    @RequestMapping("/specialArticleAddPage.do")
    public String specialArticleAddPage(){
        return "special/special/specialArticleAdd";
    }

    /**
     * 添加文章
     * @param specialLocArticle
     * @param specialLocIdStr
     * @return
     */
    @RequestMapping("/specialArticleAdd.do")
    @ResponseBody
    public Object specialArticleAdd(SpecialLocArticle specialLocArticle, String specialLocIdStr){
        try {
            String[] str = specialLocIdStr.split("_");
            String specialLocId = str[0];
            specialLocArticle.setLocId(Integer.valueOf(specialLocId));
            Date date = new Date();
            specialLocArticle.setCreateTime(date);
            specialLocArticle.setUpdateTime(date);

            baseService.save(specialLocArticle);
            return  renderSuccess("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderError("添加失败");
        }


    }

    /**
     * 到编辑文章页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/specialArticleEditPage.do")
    public String specialArticleEditPage(Model model,Integer id){
        try {
            String sql = "SELECT la.id,la.article_id, la.ord,l.id AS lid,l.type AS ltype,l.`name` AS lname,a.title AS title \n" +
                    "   FROM qy_special_loc_article la,qy_special_loc l,qy_article a  " +
                    "   WHERE la.loc_id = l.id   " +
                    "   AND la.article_id = a.id  " +
                    "   AND la.id = "+id;
            List<Map<String, Object>> locArticleList = baseService.findMapBySQL(sql);
            model.addAttribute("locArticleList",locArticleList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "special/special/specialArticleEdit";
    }

    /**
     * 编辑文章
     * @param specialLocArticle
     * @param specialLocIdStr
     * @return
     */
    @RequestMapping("/specialArticleEdit.do")
    @ResponseBody
    public Object specialArticleEdit(SpecialLocArticle specialLocArticle,String specialLocIdStr){
        try {
            String[] str = specialLocIdStr.split("_");
            String specialLocId = str[0];
            specialLocArticle.setLocId(Integer.valueOf(specialLocId));
            specialLocArticle.setUpdateTime(new Date());

            SpecialLocArticle la = baseService.get(SpecialLocArticle.class, specialLocArticle.getId());
            BeanUtil.copyProperties(specialLocArticle,la);
            baseService.update(la);

            return  renderSuccess("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderSuccess("编辑失败");
        }
    }

    /**
     * \删除文章
     * @param specialLocArticle
     * @return
     */
    @RequestMapping("/specialArticleDelete.do")
    @ResponseBody
    public Object specialArticleDelete(SpecialLocArticle specialLocArticle){
        try {
            baseService.delete(specialLocArticle);
            return  renderSuccess("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderError("删除失败");
        }
    }
}
